package com.easyn.CamLinePro;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.easyn.P2PCam264.AddDeviceActivity;
import com.easyn.command.CommonDialog;
import com.easyn.command.SearchResult;
import com.tutk.IOTC.Camera;
import com.tutk.IOTC.st_LanSearchInfo;

/**
 * 展示搜索到的与wifi相关的device
 * 
 * @author Kevin
 * 
 *         2015-11-11
 */
public class AddWifiActivity extends Activity implements OnClickListener {
	private Button mEditBtn;
	private List<SearchResult> list = new ArrayList<SearchResult>();
	private ListView mListView = null;
	private ImageView mReflesh;
	private SearchResultListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
		initData();
	}

	private void doLanSearch() {
		list.clear();
		st_LanSearchInfo[] arrResp = Camera.SearchLAN();
		if (arrResp != null && arrResp.length > 0) {
			for (st_LanSearchInfo resp : arrResp) {
				list.add(new SearchResult(new String(resp.UID).trim(),
						new String(resp.IP).trim(), (int) resp.port));
			}
		}

	}

	private void initUI() {
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.titlebar);
		TextView tv = (TextView) this.findViewById(R.id.bar_text);
		tv.setText(R.string.add_wifi_title);
		mEditBtn = (Button) this.findViewById(R.id.bar_right_btn);
		mEditBtn.setText(R.string.cancel);
		mEditBtn.setTextColor(Color.WHITE);
		mEditBtn.setVisibility(View.VISIBLE);
		mEditBtn.setOnClickListener(this);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_add_wifi);
		mListView = (ListView) findViewById(R.id.lan_search_lv);
		mReflesh = (ImageView) findViewById(R.id.iv_reflesh);
	}

	@SuppressWarnings("unchecked")
	private void initData() {
		mAdapter = new SearchResultListAdapter();
		mListView.setAdapter(mAdapter);
		new MyAsyncTask().execute("");

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				SearchResult r = list.get(position);
				intent.putExtra("uid", r.UID);
				if (AddDeviceActivity.sIsAddCameraSkip) {
					setResult(100, intent);
					AddDeviceActivity.sIsAddCameraSkip = false;

				} else {
					setResult(999, intent);
				}
				finish();
				// mUIDEdtTxt.setText(r.UID);

			}
		});
		mReflesh.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				new MyAsyncTask().execute("");
			}
		});
	}

	private class SearchResultListAdapter extends BaseAdapter {

		public int getCount() {

			return list.size();
		}

		public Object getItem(int position) {

			return list.get(position);
		}

		public long getItemId(int position) {

			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			final SearchResult result = (SearchResult) getItem(position);
			ViewHolder holder = null;

			if (convertView == null) {

				convertView = LayoutInflater.from(AddWifiActivity.this)
						.inflate(R.layout.search_device_result, null);

				holder = new ViewHolder();
				holder.uid = (TextView) convertView.findViewById(R.id.uid);
				holder.ip = (TextView) convertView.findViewById(R.id.ip);

				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}

			holder.uid.setText(result.UID);
			holder.ip.setText(result.IP);
			// holder.port.setText(result.Port);

			return convertView;
		}// getView()

		public final class ViewHolder {
			public TextView uid;
			public TextView ip;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_right_btn:
			finish();
			break;

		default:
			break;
		}

	}

	class MyAsyncTask extends AsyncTask<String, Void, List<SearchResult>> {
		@Override
		protected void onPreExecute() {
			CommonDialog.showDialog(AddWifiActivity.this);
			super.onPreExecute();
		}

		@Override
		protected List<SearchResult> doInBackground(String... params) {
			doLanSearch();
			return null;
		}

		@Override
		protected void onPostExecute(List<SearchResult> result) {
			CommonDialog.closeDialog();
			mAdapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}
	}
}
